<?php
class XA_Db_PgSQL extends XA_Db {

	public function __construct($host, $user, $password, $db_name) {
		$this->connection = pg_connect('host='.$host.' dbname='.$db_name.' user='.$user.' password='.$password);
	}


	public function escapeString($string) {
		return pg_escape_string($this->connection, $string);
	}


	public function query($query) {
		unset($this->insert_id);
		if (is_a($query, 'XA_Query')) {
			$query = $query->get();
		}
		if (($result = pg_query($this->connection, $query)) === false) {
			$exception = new Exception(pg_last_error($this->connection)."\r\n\r\n".$query."\r\n\r\n");
			header('Content-type: text/plain');
			die("\n".$exception);
		} elseif (pg_result_status($result) === PGSQL_COMMAND_OK) {
			$row = pg_fetch_row(pg_query($this->connection, "SELECT lastval()"));
			return $row[0];
		} else {
			return new XA_Data_PgSQL($result);
		}
	}


	public function getInsertId() {
		return $this->insert_id;
	}
}
